/*    */ package com.zimbra.cs.security.sasl;
/*    */ 
/*    */ import java.io.DataInputStream;
/*    */ import java.io.EOFException;
/*    */ import java.io.IOException;
/*    */ import java.io.InputStream;
/*    */ import java.nio.ByteBuffer;
/*    */ import javax.security.sasl.SaslClient;
/*    */ import javax.security.sasl.SaslServer;
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ public class SaslInputStream
/*    */   extends InputStream
/*    */ {
/*    */   private final DataInputStream mInputStream;
/*    */   private final SaslSecurityLayer mSecurityLayer;
/*    */   private ByteBuffer mBuffer;
/*    */   private static final boolean DEBUG = false;
/*    */   
/*    */   public SaslInputStream(InputStream is, SaslServer server)
/*    */   {
/* 36 */     this(is, SaslSecurityLayer.getInstance(server));
/*    */   }
/*    */   
/*    */   public SaslInputStream(InputStream is, SaslClient client) {
/* 40 */     this(is, SaslSecurityLayer.getInstance(client));
/*    */   }
/*    */   
/*    */   public SaslInputStream(InputStream is, SaslSecurityLayer securityLayer) {
/* 44 */     this.mInputStream = new DataInputStream(is);
/* 45 */     this.mSecurityLayer = securityLayer;
/*    */   }
/*    */   
/*    */   public int read(byte[] b, int off, int len) throws IOException {
/* 49 */     debug("read: enter len = %d", new Object[] { Integer.valueOf(len) });
/* 50 */     if ((off | len | off + len | b.length - (off + len)) < 0)
/* 51 */       throw new IndexOutOfBoundsException();
/* 52 */     if (len == 0) {
/* 53 */       return 0;
/*    */     }
/* 55 */     if (!ensureBuffer()) return -1;
/* 56 */     if (len > this.mBuffer.remaining()) len = this.mBuffer.remaining();
/* 57 */     this.mBuffer.get(b, off, len);
/* 58 */     debug("read: exit len = %d", new Object[] { Integer.valueOf(len) });
/* 59 */     return len;
/*    */   }
/*    */   
/*    */   public int read() throws IOException {
/* 63 */     return ensureBuffer() ? this.mBuffer.get() : -1;
/*    */   }
/*    */   
/*    */   private boolean ensureBuffer() throws IOException
/*    */   {
/* 68 */     if ((this.mBuffer != null) && (this.mBuffer.hasRemaining())) return true;
/*    */     do {
/* 70 */       if (!fillBuffer()) return false;
/* 71 */     } while (!this.mBuffer.hasRemaining());
/* 72 */     return true;
/*    */   }
/*    */   
/*    */   private boolean fillBuffer() throws IOException {
/*    */     int len;
/*    */     try {
/* 78 */       len = this.mInputStream.readInt();
/*    */     } catch (EOFException e) {
/* 80 */       return false;
/*    */     }
/* 82 */     debug("fillBuffer: len = %d", new Object[] { Integer.valueOf(len) });
/*    */     
/* 84 */     if (len < 0) {
/* 85 */       throw new IOException("Invalid buffer length in stream");
/*    */     }
/* 87 */     byte[] b = new byte[len];
/* 88 */     this.mInputStream.readFully(b);
/* 89 */     this.mBuffer = ByteBuffer.wrap(this.mSecurityLayer.unwrap(b, 0, len));
/* 90 */     debug("fillBuffer: read finished", new Object[0]);
/* 91 */     return true;
/*    */   }
/*    */   
/*    */   public void close() throws IOException {
/* 95 */     this.mInputStream.close();
/* 96 */     this.mBuffer = null;
/*    */   }
/*    */   
/*    */   private static void debug(String format, Object... args) {}
/*    */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/security/sasl/SaslInputStream.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */